﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AMOOF {
    public class IOProcessor {
        // F:\AMOOF_REP\Project\Jim\AMOOF\AMOOF\Peri_Interface\testNewRandomizer_double.txt
        public static void writeToFile(string filename, string text) {
            string directory = PeriInterface.periFolderDirectory;
            TextWriter tw = new StreamWriter(directory + "\\" + filename + ".txt");
            tw.WriteLine(text);
            tw.Close();
        }

        public static DataPoint[] loadDataPoints(string path) {
            LinkedList<DataPoint> data = new LinkedList<DataPoint>();

            // Open the file to read from.
            using (StreamReader sr = File.OpenText(path)) {
                string s = "";
                iterateToLine("<StartData>", sr);
                while ((s = sr.ReadLine()) != null) {
                    DataPoint thisPoint = new DataPoint();
                    s.Trim();
                    string[] words = s.Split(',');
                    if (!words.ElementAt(0).Equals("")) {
                        thisPoint.x = Convert.ToDouble(words[0].Trim());
                        if (words.Length > 1) {
                            thisPoint.y = Convert.ToDouble(words[1].Trim());
                        }
                        else {
                            thisPoint.y = 1;
                        }
                        data.AddLast(thisPoint);
                    }
                }
                return data.ToArray();
            }
        }

        public static void iterateToLine(string value, StreamReader sr) {
            while (!(sr.ReadLine()).Trim().Equals(value)) {
            }
        }
    }
}
